package com.shensi.adminCollect.service.label.impl;

import com.alibaba.fastjson.JSONObject;
import com.shensi.adminCollect.common.constant.UserConstants;
import com.shensi.adminCollect.common.exception.CustomException;
import com.shensi.adminCollect.domain.label.ButtonSwitch;
import com.shensi.adminCollect.mapper.label.ButtonSwitchMapper;
import com.shensi.adminCollect.service.label.IButtonSwitchService;
import com.shensi.adminCollect.mapper.system.SysUserMapper;
import com.shensi.common.core.domain.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;



import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;

@Slf4j
@Service
public class IButtonSwitchServiceImpl implements IButtonSwitchService {

    @Resource
    private ButtonSwitchMapper buttonSwitchMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Transactional
    @Override
    public ButtonSwitch getButtonSwitch(SysUser sysUser) {
        if(Objects.isNull(sysUser) || Objects.isNull(sysUser.getUserId())){
            throw new CustomException("getButtonSwitch请求参数异常");
        }
        log.info("getButtonSwitch查询按钮开关信息：[{}]",JSONObject.toJSONString(sysUser));
        // 判断用户等级 空的用户等级是admin
        if(Objects.isNull(sysUser.getUserLevel()) || UserConstants.LEVEL_ONE.equals(sysUser.getUserLevel())){
            return getButtonSwitch(sysUser.getUserId(),null);
        } else if(UserConstants.LEVEL_TWO.equals(sysUser.getUserLevel())){
            // 油企账号 针对一个油企可以有多个油企账号
            SysUser user = sysUserMapper.selectUserById(sysUser.getUserId());
            ButtonSwitch buttonSwitch = buttonSwitchMapper.selectByEnterpriseId(user.getEnterpriseId());
            if(Objects.isNull(buttonSwitch)){
                ButtonSwitch button = new ButtonSwitch();
                button.setUserId(user.getUserId());
                button.setEnterpriseId(user.getEnterpriseId());
                buttonSwitchMapper.insertSelective(button);
                buttonSwitch = buttonSwitchMapper.selectByEnterpriseId(user.getEnterpriseId());
            }
            return buttonSwitch;
        } else if(UserConstants.LEVEL_THREE.equals(sysUser.getUserLevel())){

            // 查询所属油企按钮权限
            SysUser user = sysUserMapper.selectUserById(sysUser.getUserId());
            ButtonSwitch EnterpriseButtonSwitch = buttonSwitchMapper.selectByEnterpriseId(user.getEnterpriseId());
            if(Objects.nonNull(EnterpriseButtonSwitch)){
                log.info("getButtonSwitch查询按钮开关信息结果：[{}]", JSONObject.toJSONString(EnterpriseButtonSwitch));
                return EnterpriseButtonSwitch;
            }

            // 油企权限不存在时，获取油企账号
            SysUser enterpriseUser = sysUserMapper.selectByEnterpriseIdAndUserLevel(user.getEnterpriseId(),UserConstants.LEVEL_TWO);

            ButtonSwitch button = new ButtonSwitch();
            button.setUserId(enterpriseUser.getUserId());
            button.setEnterpriseId(enterpriseUser.getEnterpriseId());
            buttonSwitchMapper.insertSelective(button);
            ButtonSwitch buttonSwitch = buttonSwitchMapper.selectByEnterpriseId(enterpriseUser.getEnterpriseId());
            return buttonSwitch;

        }

        // 局域经理、业务员，返回默认结果
        ButtonSwitch buttonSwitch = new ButtonSwitch();
        buttonSwitch.setUserId(sysUser.getUserId());
        buttonSwitch.setHomeAll("1");
        buttonSwitch.setHomeToday("0");
        buttonSwitch.setHomeYesterday("0");
        buttonSwitch.setHomeWeek("0");
        buttonSwitch.setHomeMonth("0");
        buttonSwitch.setHomeCustom("0");
        log.info("getButtonSwitch查询按钮开关信息结果：[{}]", JSONObject.toJSONString(buttonSwitch));
        return buttonSwitch;
    }

    private ButtonSwitch getButtonSwitch(Long userId, Long enterpriseId){
        ButtonSwitch buttonSwitch = buttonSwitchMapper.selectByPrimaryKey(userId);
        if(Objects.isNull(buttonSwitch)){
            ButtonSwitch button = new ButtonSwitch();
            button.setUserId(userId);
            button.setEnterpriseId(enterpriseId);
            buttonSwitchMapper.insertSelective(button);
            buttonSwitch = buttonSwitchMapper.selectByPrimaryKey(userId);
        }
        log.info("getButtonSwitch查询按钮开关信息结果：[{}]", JSONObject.toJSONString(buttonSwitch));
        return buttonSwitch;
    }

    @Override
    public int updateButtonSwitch(ButtonSwitch buttonSwitch) {
        if(Objects.isNull(buttonSwitch) || Objects.isNull(buttonSwitch.getUserId())){
            throw new CustomException("updateButtonSwitch请求参数异常");
        }
        buttonSwitch.setUpdateTime(new Date());
        int num = buttonSwitchMapper.updateByPrimaryKeySelective(buttonSwitch);
        return num;
    }
}
